"""
视界智检系统 - 主应用入口
重构后的简洁版本，采用模块化架构
"""
from flask import Flask, render_template
from flask_socketio import SocketIO
import threading

# 导入配置
from config.app_config import FLASK_CONFIG

# 导入路由
from routes.system_routes import system_bp
from routes.config_routes import config_bp
from routes.log_routes import log_bp
from routes.statistics_routes import statistics_bp
from routes.video_stream import video_bp

# 导入服务
from services.detection_service import DetectionService
from services.system_service import system_service

# 导入WebSocket处理
from websocket.socket_handlers import register_socket_handlers

# 全局检测服务实例
detection_service_instance = None

from services.voice_service import main as voice_main

def create_app():
    """创建Flask应用"""
    app = Flask(__name__)
    app.config.update(FLASK_CONFIG)
    
    # 注册路由
    app.register_blueprint(system_bp)
    app.register_blueprint(config_bp)
    app.register_blueprint(log_bp)
    app.register_blueprint(statistics_bp)
    app.register_blueprint(video_bp)
    
    # 主页面路由
    @app.route('/')
    def index():
        """主页面"""
        return render_template('index.html')
    
    @app.route('/stats')
    def stats_page():
        """统计页面"""
        return render_template('stats.html')
    
    return app

# def create_socketio_app():
#     """创建SocketIO应用"""
#     global detection_service_instance

#     print("创建SocketIO应用")
    
#     app = create_app()
#     socketio = SocketIO(app, cors_allowed_origins="*")
    
#     # 初始化检测服务
#     detection_service_instance = DetectionService(socketio)
    
#     # 更新路由中的检测服务引用
#     import services.detection_service
#     services.detection_service.detection_service = detection_service_instance
    
#     import routes.system_routes
#     routes.system_routes.detection_service = detection_service_instance
    
#     # 注册WebSocket事件处理器
#     register_socket_handlers(socketio)
    
#     return app, socketio

def create_socketio_app():
    """创建SocketIO应用"""
    global detection_service_instance

    print("创建SocketIO应用")
    
    app = create_app()
    socketio = SocketIO(app, cors_allowed_origins="*")
    
    # 初始化检测服务
    detection_service_instance = DetectionService(socketio)
    
    # 更新路由中的检测服务引用
    import services.detection_service
    services.detection_service.detection_service = detection_service_instance
    
    import routes.system_routes
    routes.system_routes.detection_service = detection_service_instance
    routes.system_routes.socketio = socketio
    
    # 注册WebSocket事件处理器
    register_socket_handlers(socketio)
    
    return app, socketio

if __name__ == '__main__':
    print("=" * 50)
    print("视界智检系统启动中...")
    print("项目已重构为模块化架构")
    print("=" * 50)
    print("🔧 后端架构:")
    print("   ├── 配置管理: config/")
    print("   ├── 路由模块: routes/")
    print("   ├── 业务服务: services/")
    print("   └── WebSocket: websocket/")
    print("=" * 50)
    print("🌐 请在浏览器中访问: http://172.16.250.167:5000")
    print("=" * 50)

    # 启动语音控制监听线程
    print("启动语音控制线程...")
    voice_thread = threading.Thread(target=voice_main, daemon=True)
    voice_thread.start()
    
    app, socketio = create_socketio_app()
    socketio.run(app, host='0.0.0.0', port=5000, debug=True) 